import { DICT_KEY } from "@/enums/cacheEnum";
const useDictStore = defineStore("dict", () => {
	// 字典数据
	const dictData = ref(cache.get(DICT_KEY));
	function getData(dictValue) {
		if (!dictData) {
			return [];
		}
		const tree = dictData.value.find((item) => item.dictValue === dictValue);
		if (tree) {
			// 预约表状态特殊处理
			if (dictValue === "RESERVATION_STATUS") {
				let params = {};
				tree.children.map((item) => {
					params[item["dictValue"]] = {
						value: item["dictValue"],
						label: item["name"],
					};
				});
				params["ALL"] = {
					value: "ALL",
					label: "全部",
				};
				return params;
			} else {
				return tree.children.map((item) => {
					return {
						value: item["dictValue"],
						label: item["name"],
						kid: item["children"]?.map((e) => {
							return {
								value: e["dictValue"],
								label: e["name"],
							};
						}) || [],
					};
				});
			}
		}
		return [];
	}
	// 存取数据
	function setData(data) {
		dictData.value = data;
		cache.set(DICT_KEY, dictData.value);
	}
	return {
		setData,
		getData,
	};
});
export default useDictStore;
